// pages/particulars/particulars.js
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    details: [],
    openid: "",
    username: "",
    userimg: "",
    userjudge:"canyu",
    whenday2:"",
    realityday:"",
    dakatab:''
  },

  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openid)
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
        console.log("保存的openid", that.data.openid)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("hdksajdlak",options)
    var id = options
    this.setData({
      id: id
    })
    console.log(this.data.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  saveplan: function () {
    if (this.data.openid == this.data.details._openid) {
      wx.showToast({
        title: '创建者本人不能添加',
        icon: 'none',
        duration: 2000
      })
      return;
    } else {
      console.log('点击事件执行了')
      wx.getUserProfile({
        desc: '必须授权才能使用',
        success: res => {
          var username = res.userInfo.nickName
          var userimg = res.userInfo.avatarUrl
          var realityday = this.data.realityday
          var dakatab = this.data.dakatab
          if(this.data.realityday == 0){
            realityday = this.data.realityday
          }else{
            if(dakatab == false){
              realityday = this.data.realityday
            }else{
              realityday = this.data.realityday-=1
            }
          }
          console.log(realityday)
          console.log(username)
          console.log(userimg)
          console.log('成功', res)
          this.setData({
            username: username,
            userimg: userimg
          })
          wx.cloud.callFunction({
            // 云函数名称【刚刚创建的云函数文件的名字】
            name: 'addopenid',
            // 传给云函数的参数　　 // dataId 为我要修改的 users 集合内数据的 _id ； day 为要修改的内容
            data: {
              dataId: this.data.id.id,
              openid: this.data.openid,
              username: this.data.username,
              userimg: this.data.userimg,
              userjudge: this.data.userjudge,
              whenday2: this.data.whenday2,
              realityday: realityday
            },
            success: function (res) {
              console.log(res)
              wx.switchTab({
                url: '/pages/shouye/shouye',
              })
            },
          })
        },
        fall: res => {
          console.log('失败', res)
        }
      })
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOpenid();
    db.collection('jihua').doc(this.data.id.id).get({
      success: res => {
        this.setData({
          details: res.data,
          whenday2: res.data.whenday2,
          realityday: res.data.realityday,
          dakatab: res.data.dakatab
        })
        console.log(this.data.details)
        console.log(this.data.whenday2)
        console.log(this.data.realityday)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})